package model

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ PmsCategoryModel = (*customPmsCategoryModel)(nil)

type (
	// PmsCategoryModel is an interface to be customized, add more methods here,
	// and implement the added methods in customPmsCategoryModel.
	PmsCategoryModel interface {
		pmsCategoryModel
		FindAll(ctx context.Context) ([]*PmsCategory, error)
		DeleteBatch(ctx context.Context, catIdList []int64) error
	}

	customPmsCategoryModel struct {
		*defaultPmsCategoryModel
	}
)

// NewPmsCategoryModel returns a model for the database table.
func NewPmsCategoryModel(conn sqlx.SqlConn) PmsCategoryModel {
	return &customPmsCategoryModel{
		defaultPmsCategoryModel: newPmsCategoryModel(conn),
	}
}

// DeleteBatch oft delete
func (m *defaultPmsCategoryModel) DeleteBatch(ctx context.Context, catIdList []int64) error {
	str := ""
	for _, v := range catIdList {
		str += fmt.Sprintf("%d,", v)
	}
	str = str[:len(str)-1]
	query := fmt.Sprintf("UPDATE %s SET pms_category.show_status = FALSE WHERE cat_id IN (%s)", m.table, str)
	_, err := m.conn.ExecCtx(ctx, query)
	return err
}

func (m *defaultPmsCategoryModel) FindAll(ctx context.Context) ([]*PmsCategory, error) {
	var resp []*PmsCategory
	query := fmt.Sprintf("select %s from %s", pmsCategoryRows, m.table)
	err := m.conn.QueryRowsCtx(ctx, &resp, query)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}
